package cn.ecnu.controller.forum;


import cn.ecnu.common.utils.R;
import cn.ecnu.pojo.forum.Comment;
import cn.ecnu.service.forum.CommentService;
import cn.ecnu.vo.forum.CommentViewVo;
import cn.ecnu.vo.forum.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/circle/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    // TODO: 后面最好吧所有的uid都替换成user


    @PostMapping("/commentPost")
    public R commentAnPost(@RequestBody CommentVo commentVo) {
        try {
            Comment comment = commentService.commentAnPost(commentVo);
            return R.ok().put("data", comment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @PostMapping("/replyComment")
    public R commentAnComment(@RequestBody CommentVo commentVo) {
        try {
            Comment comment = commentService.commentAnComment(commentVo);
            return R.ok().put("data", comment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getCommentByPostId/{postId}")
    public R getCommentByPostId(@PathVariable("postId") Integer postId) {
        try {
            List<Comment> comments = commentService.getCommentByPostId(postId);
            return R.ok().put("data", comments);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getCommentByPostIdOrderByHot/{postId}")
    public R getCommentByPostIdOrderByHot(@PathVariable("postId") Integer postId) {
        try {
            List<Comment> comments = commentService.getCommentByPostIdOrderByHot(postId);
            return R.ok().put("data", comments);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getCommentByCommentId/{commentId}")
    public R getCommentByCommentId(@PathVariable("commentId") Integer commentId) {
        try {
            List<Comment> comments = commentService.getCommentByCommentId(commentId);
            return R.ok().put("data", comments);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }


    @GetMapping("/getCommentByCommentIdOrderByHot/{postId}")
    public R getCommentByCommentIdOrderByHot(@PathVariable("postId") Integer postId) {
        try {
            List<Comment> comments = commentService.getCommentByCommentIdOrderByHot(postId);
            return R.ok().put("data", comments);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/deleteCommentById/{commentId}")
    public R deleteCommentById(@PathVariable("commentId") Integer commentId) {
        try {
            commentService.deleteCommentById(commentId);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }





}
